package mysql

import (
	"gorm.io/gorm"
	"video-system/userrpc/userrpc"
)

type CommentInfo struct {
	gorm.Model
	VideoId        int64  `gorm:"type:int(11);not null;comment:'视频ID'"`
	UserId         int64  `gorm:"type:int(11);not null;comment:'用户ID'"`
	CommentContent string `gorm:"type:varchar(500);not null;comment:'评论内容'"`
	IsAnonymous    int64  `gorm:"type:tinyint;not null;default:0;comment:'是否匿名'"`
	CommentStatus  int64  `gorm:"type:int(11);not null;default:0;comment:'评论状态'"`
	ReplyId        int64  `gorm:"type:int(11);comment:'回复ID'"`
}

func (i *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CommentInfo) CommentDel(db *gorm.DB, in *userrpc.CommentDelRequest) error {
	return db.Debug().Where("comment_infos.user_id = ?", in.UserId).Where("comment_infos.video_id = ?", in.VideoId).Delete(i).Error
}

func CommentList(db *gorm.DB, in *userrpc.CommentListRequest) []*userrpc.CommentList {
	var list []*userrpc.CommentList
	db.Debug().Model(&CommentInfo{}).Select("comment_infos.*,user_infos.nick_name,user_infos.avatar_link").
		Joins("left join user_infos on user_infos.id = comment_infos.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("created_at desc").
		Find(&list)
	return list
}
